import { URL_REGISTER_CHECK } from "@/comm/api";
import { post } from "@/comm/request";
import { maskEmail } from "@/comm/tooler";
import { defineStore } from "pinia";

// interface IState {
//     email: string;
//     dynamicCode: string;
//     recommendCode: string;
//     popLogin: boolean;
//     popRegister: boolean;
//     popLoginVerify: boolean;
//     popRegisterVerify: boolean;
//     popRegisterSuccess: boolean;
//     popInvite: boolean;
//     popSubscribe: boolean;
//     popBindFacebook: boolean;
//     popBindTwitter: boolean;
//     popBindGoogle: boolean;
//     popShare: boolean;
//     isRegister: boolean;
//     isBindEmail: boolean;
//     canLook: boolean;
//     joinToken: any;
//     isMetaMask: boolean
// }

const initState = {
    email: "",
    dynamicCode: "",
    recommendCode: "",
    popConnect:false,
    popLogin: false,
    popRegister: false,
    popLoginVerify: false,
    popRegisterVerify: false,
    popRegisterSuccess: false,
    popInvite: false,
    popSubscribe: false,
    popBindFacebook: false,
    popBindTwitter: false,
    popBindGoogle: false,
    popShare: false,
    isRegister: false,
    isBindEmail: false,
    canLook: true,
    joinToken: null as any,
    isMetaMask: false,
    isQuickReg:false,//是否是快速注册(邮箱不存在立即跳转到注册
}

type IState = typeof initState;

export const useAccount = defineStore("account", {
    state: (): IState => initState,

    getters: {
        saveEmail(state) {
            return maskEmail(state.email);
            // const ary = state.email.split("@");
            // if (ary.length !== 2) {
            //     return ary[0];
            // }
            // return ary[0].substring(0, 3) + "***@" + ary[1];
        }
    },
    actions: {
        changeQuickReg(val:boolean){
            this.isQuickReg = val;
        },
        // 判断用户是否安装了metamask
        isMetaMaskInstall() {
            this.isMetaMask = (window as any).ethereum && (window as any).ethereum.isMetaMask
        },
        changeJoinToken(val: string) {
            this.joinToken = val;
        },
        changeEmail(val: string) {
            this.email = val;
        },
        changeCanLook(val: boolean) {
            console.log("----------------- lock", val);
            this.canLook = val;
        },
        changeIsBindEmail(val: boolean) {
            console.log("isBindEmail====", val);
            this.isBindEmail = val;
        },
        changeDynamicCode(val: string) {
            this.dynamicCode = val;
        },
        changeRecommendCode(val: string) {
            this.recommendCode = val;
        },
        changePopConnect(val: boolean) { 
            this.popConnect = val;
        },
        changePopLogin(val: boolean) {
            this.popLogin = val;
        },
        changePopRegister(val: boolean) {
            this.popRegister = val;
        },
        changePopLoginVerify(val: boolean) {
            this.popLoginVerify = val;
        },
        changePopRegisterVerify(val: boolean) {
            this.popRegisterVerify = val;
        },
        changePopRegisterSuccess(val: boolean) {
            this.popRegisterSuccess = val;
            localStorage.setItem("reward", val ? "1" : "0")
        },
        changePopInvite(val: boolean) {
            this.popInvite = val;
        },
        changePopSubscribe(val: boolean) {
            this.popSubscribe = val;
        },

        changePopBindFacebook(val: boolean) {
            this.popBindFacebook = val;
        },
        changePopBindTwitter(val: boolean) {
            this.popBindTwitter = val;
        },
        changePopBindGoogle(val: boolean) {
            this.popBindGoogle = val;
        },
        changePopShare(val: boolean) {
            this.popShare = val;
        },
        changeIsRegister(val: boolean) {
            this.isRegister = val;
        },
        async checkEmail(val: string) {
            let res: any = await post(URL_REGISTER_CHECK, { email: val }, true);
            if (res.code == 200) {
                return null;
            }
            return res.info;
        }
    }
});
